
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


public class IGui {
 JButton b1,b2,b3,b4,b5;
 JFrame f;
 JPanel p1;
 JLabel l1;
 AddInventory ai=new AddInventory();
 AddProduct ap=new AddProduct();
 ProductInventory pi=new ProductInventory();

	public IGui()
	{
		f=new JFrame("SUPPLY CHAIN MANAGEMENT SYSTEM");
		f.setLayout(new BorderLayout());

		l1=new JLabel("SUPPLY CHAIN MANAGEMENT SYSTEM");
		l1.setFont(new Font("Comic Sans ms", Font.BOLD, 23));
		l1.setForeground(Color.red);

		f.add(l1,BorderLayout.NORTH);
		b1=new JButton("View Manufacturer Product List");
		b2=new JButton("Inventory Report for Manufacturing a Product");
		b3=new JButton("View Currently Available Inventory Items");
		b4=new JButton("Add New Product");
		b5=new JButton("Add New Inventory Item");


		p1=new JPanel(new GridLayout(5,1));
		p1.add(b1);
		p1.add(b2);
		p1.add(b3);
		p1.add(b4);
		p1.add(b5);
		f.add(p1,BorderLayout.CENTER);

		b1.addActionListener((ActionListener) this);
		b2.addActionListener((ActionListener) this);
		b3.addActionListener((ActionListener) this);
		b4.addActionListener((ActionListener) this);
		b5.addActionListener((ActionListener) this);

		f.setVisible(true);
		f.setSize(500,500);
		f.setLocation(200,200);
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}



	public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("View Manufacturer Product List")) viewProducts();
	if (event.getActionCommand().equals("Inventory Report for Manufacturing a Product")) pi.displayInventory();
        if (event.getActionCommand().equals("View Currently Available Inventory Items")) currentInventory();
	if (event.getActionCommand().equals("Add New Product")) ap.addProduct();
	if (event.getActionCommand().equals("Add New Inventory Item")) ai.addInventory();

	}



	public void viewProducts()
	{

         try {

              XMLDecoder istream = new XMLDecoder(new BufferedInputStream(new FileInputStream("products.xml")));
              ProductList obj =(ProductList) istream.readObject();
                istream.close();

	Product p=new Product();

	Object[] col = new Object[]{"Name","ProductId","Quantity","Description","Inventory List"};
	Object[][] data = {};
	DefaultTableModel model = new DefaultTableModel(data,col);
	JTable table = new JTable(model);
	for(int i=0;i<obj.products.size();i++)
	{
	p=(Product) obj.products.get(i);
	Object[] row1=new Object[]{p.getName(),p.getProductid(),p.getQuantity(),p.getDescription(),p.getInvenlist()};
	model.addRow(row1);
	}
	JScrollPane pane = new JScrollPane( table );
	JFrame jfr=new JFrame("Available Products List");
	jfr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	jfr.setBackground(Color.white);
	jfr.setSize(600,300);
        jfr.setVisible(true);
	jfr.setLocation(200,200);
	jfr.add(pane);

	}
       catch(Exception ex) {
         System.out.println(ex);
         }
	}




	public void currentInventory()
	{
	   try {

              XMLDecoder istream = new XMLDecoder(new BufferedInputStream(new FileInputStream("inventory.xml")));
              InventoryList obj =(InventoryList) istream.readObject();
              istream.close();

		Inventory p=new Inventory();

		Object[] col = new Object[]{"Name","InventoryId","Quantity","Price"};
		Object[][] data = {};
		DefaultTableModel model = new DefaultTableModel(data,col);
		JTable table = new JTable(model);
		for(int i=0;i<obj.inventories.size();i++)
		{
		p=(Inventory) obj.inventories.get(i);
		Object[] row1=new Object[]{p.getName(),p.getInventoryid(),p.getQuantity(),p.getPrice()};
		model.addRow(row1);
		}
		JScrollPane pane = new JScrollPane( table );
		JFrame jfr=new JFrame("Currently Available Inventory List");
		jfr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jfr.setBackground(Color.white);
		jfr.setSize(600,300);
        	jfr.setVisible(true);
		jfr.setLocation(200,200);
		jfr.add(pane);

		}
       	    catch(Exception ex) {
         	System.out.println(ex);
         	}
	}


	public static void main(String args[])
	{
	IGui i=new IGui();
	}
}
